import times
import gapbuffer

# Global stats
var
  totalTime = 0.0
  numBenches = 0

template benchmark(name: string, body: untyped) =
  var t = 0.0
  for i in 0..100000:
    var start = epochTime()
    block:
      body

    t += epochTime() - start

  totalTime += t
  numBenches += 1

  echo "Benchmark `", name, "` took ~", int(t * 1000), "ms"

benchmark "from 16 character string":
  discard "abcdefghijklmnop".toGapBuffer()

var buf = "abcdefghijklmnop".toGapBuffer()
benchmark "to 16 character string":
  discard $buf

benchmark "get position":
  discard buf.pos()

benchmark "get length":
  discard buf.len()

benchmark "move to":
  buf.moveTo(10)
  buf.moveTo(0)

benchmark "move by":
  buf.move(10)
  buf.moveTo(0)

benchmark "insert/delete":
  buf.insert('a')
  buf.delete()

benchmark "replace at cursor":
  buf[buf.pos()] = 'a'

benchmark "replace elsewhere":
  buf[10] = 'a'

benchmark "insert string":
  buf.insert("hello\n")

benchmark "split lines":
  var i = 0
  for _ in buf.splitLines():
    break

echo()
echo numBenches, " benchmarks run"
echo "Total time:\t~", int(totalTime * 1000), "ms"
echo "Average time:\t~", int((totalTime * 1000) / float(numBenches)), "ms"

